package com.burgeon.backend.SHOPBACKEND.rest;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.Classification;
import com.burgeon.backend.SHOPBACKEND.entity.FAB.FABRelevance;
import com.burgeon.backend.SHOPBACKEND.entity.FAB.FabEntity;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 根据款号查询商品信息
 */
public class QueryFABGoodsByCode extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public QueryFABGoodsByCode(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 接收REP返回的字符串并进行数据处理
     *
     * @return
     */
    public FABRelevance getData(String code) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(code), url);
        String code1 = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code1)) {
            throw new Exception(str);
        }
        System.err.println(str);
        int count = new JSONArray(str).getJSONObject(0).getInt("count");
        if (count==0){
            return null;
        }
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        FabEntity fabEntity = new FabEntity();
        String goodcode = jsonArray.getJSONArray(0).getString(0);
        FABRelevance fabRelevance = new FABRelevance();
        fabRelevance.setCode(goodcode);
        fabRelevance.setName(jsonArray.getJSONArray(0).getString(1));
        fabRelevance.setPrice(jsonArray.getJSONArray(0).getString(2));
        fabRelevance.setErpImg("http://"+ip+"/html/M_PRODUCT/"+goodcode+".jpg");
        /*fabEntity.setCode(goodcode);
        fabEntity.setName(jsonArray.getJSONArray(0).getString(1));
        fabEntity.setPrice(jsonArray.getJSONArray(0).getString(2));
        fabEntity.setErpImg("http://"+ip+"/html/M_PRODUCT/"+goodcode+".jpg");*/

        return fabRelevance;
    }

    /**
     * 准备向ERP传送字段拼接
     *
     * @return
     */
    private MultiValueMap Data(String code) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("NAME");//款号
        columns.add("VALUE");// 品名

        columns.add("PRICELIST");//成本价

        params.put("columns", columns);
        params.put("table", "M_PRODUCT");//表名

        HashMap paramss = new HashMap();
        paramss.put("column", "'NAME'");
        paramss.put("condition", code);
        params.put("count","true");
        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    /**
     * 执行方法，向ERP传参
     *
     * @param args
     */
    public static void main(String[] args) {
        QueryFABGoodsByCode q = new QueryFABGoodsByCode("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(q.getData("A2201CA111"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}